<template>
    <view class="orders">
        <u-tabs
            :list="tabList"
            lineColor="#FC721A"
            :activeStyle="{
                color: '#FC721A',
                fontWeight: 'bold',
                transform: 'scale(1.05)',
            }"
            :inactiveStyle="{
                color: '#1C120D',
                transform: 'scale(1)',
            }"
            @click="handleTab"
        ></u-tabs>
        <ul v-if="list.length > 0">
            <li
                v-for="(item, index) in list"
                :key="index"
                @click.stop="handleDetails(item)"
            >
                <dl>
                    <dt>
                        <image :src="item.logo" mode=""></image>
                    </dt>
                    <dd>
                        <view class="orders_title">
                            <p>{{ item.shopname }}</p>
                            <text v-if="item.state == 0">Pending Payment</text>
                            <text v-if="item.state == 1">To be used</text>
                            <text v-if="item.state == 2">To be evaluated</text>
                            <text style="color: #000" v-if="item.state == 3"
                                >Completed</text
                            >

                            <text style="color: #000" v-if="item.state == '-2'"
                                >Refunded</text
                            >
                            <text style="color: gray" v-if="item.state == '-3'"
                                >Canceled</text
                            >
                        </view>
                        <view class="orders_item">
                            <p>
                                Payment amount:
                                <text>{{ item.pay_money }}Egp</text>
                            </p>
                            <!-- <text>x1</text> -->
                        </view>
                        <p>Original amount: {{ item.origin_money }}Egp</p>
                        <p>Discount amount:{{ item.discount_money }}Egp</p>
                        <p>
                            Coupon deduction amount: {{ item.coupon_money }}Egp
                        </p>
                        <p class="orders_time">{{ item.ctime }}</p>
                    </dd>
                </dl>
            </li>
            <u-loadmore v-if="hasMore" :status="loadStatus" />
        </ul>
        <view class="empty" v-else>
            <image src="@/static/list.png" mode=""></image>
            <p>No orders yet</p>
        </view>
    </view>
</template>

<script>
import { getMyOrder } from "@/lib/business.js";
export default {
    data() {
        return {
            list: [],
            loadStatus: "loadmore",
            pageNo: 1,
            pageSize: 10,
            hasMore: false,
            state: "",
            tabList: [
                {
                    name: "All Orders",
                    state: "",
                },
                {
                    name: "To be used",
                    state: "1",
                },
                {
                    name: "To be evaluated",
                    state: "2",
                },
                // {
                //     name: "Pending refund",
                //     state: "-1",
                // },
            ],
        };
    },
    onLoad() {
        this.loadData(true);
    },
    onReachBottom() {
        if (this.hasMore) {
            this.pageNo = this.pageNo + 1;
            this.loadData();
        }
    },
    methods: {
        loadData(isReset) {
            let _this = this;
            this.loadStatus = "loading";
            let obj = {
                pageNo: _this.pageNo,
                pageSize: _this.pageSize,
            };
            if (_this.state) {
                obj["state"] = _this.state;
            }
            getMyOrder(obj)
                .then((res) => {
                    uni.hideLoading();
                    this.loadStatus = "no-more";
                    this.hasMore = res.data.hasMore;
                    if (isReset) {
                        this.list = res.data.records;
                    } else {
                        this.list = this.list.concat(res.data.records);
                    }
                })
                .catch((res) => {
                    this.loadStatus = "no-more";
                    uni.hideLoading();
                    showFailure();
                });
        },
        handleTab(e) {
            console.log(e);
            this.state = e.state;
            this.loadData(true);
        },
        handleDetails(item) {
            uni.navigateTo({
                url: "./order_details?orderid=" + item.orderid,
            });
        },
    },
};
</script>

<style lang="scss" scoped>
ul {
    padding: 0 32rpx;

    li {
        padding: 24rpx 0;

        dl {
            display: flex;
            border-bottom: 1rpx solid #f2f2f2;

            dt {
                width: 108rpx;
                height: 108rpx;
                background-color: #f2f2f2;
                border-radius: 12rpx;
                margin-right: 20rpx;
                margin-top: 10rpx;

                image {
                    width: 100%;
                    height: 100%;
                }
            }

            dd {
                flex: 1;

                .orders_title {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;

                    p {
                        font-size: 32rpx;
                        font-weight: bold;
                        color: #1c120d;
                    }

                    text {
                        font-size: 28rpx;
                        font-weight: 400;
                        color: #fc721a;
                    }
                }

                .orders_item {
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                    margin-top: 12rpx;

                    p {
                        font-size: 24rpx;
                        font-weight: 400;
                        color: #444444;

                        text {
                            font-size: 28rpx;
                            font-weight: 600;
                        }
                    }

                    > text {
                        font-size: 28rpx;
                        font-weight: 400;
                        color: #9e6b47;
                    }
                }

                p {
                    font-size: 24rpx;
                    font-weight: 400;
                    color: #444444;
                    margin-bottom: 10rpx;
                }

                .orders_time {
                    font-size: 24rpx;
                    font-weight: 400;
                    color: #888888;
                    margin-top: 10rpx;
                }
            }
        }
    }
}

.empty {
    text-align: center;
    margin-top: 10vh;

    image {
        width: 376rpx;
        height: 376rpx;
    }

    p {
        font-size: 28rpx;
        color: #999999;
    }
}
</style>
